YAHOO.namespace("gosearch.go.gameHall");

(function() {
    var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event, Sel = YAHOO.util.Selector, q = Sel.query;
    var VIEW_URL = "/GoSearch/phppages/";
    var SERVICE_URL = "/GoSearch/phpsrc/";

    YAHOO.gosearch.go.gameHall.GameHall = function() {
        this.container = document.createElement("div");
    }
    var gameHall = YAHOO.gosearch.go.gameHall.GameHall;
    gameHall.prototype = {

        /**
		 * render html in some other container.
		 * 
		 * @return an element with html code from gameHall.php
		 */
        render : function(containerEL) {
            var that = this;
            var loadViewSuccess = function(props) {
                containerEL.innerHTML = props.responseText;
                that.containerinnerHTML = props.responseText;
                Event.addListener(q(".login-submit"), "click", function(e) {
                    YAHOO.gosearch.app.getGameHall().login(e);
                });
                Event.addListener(q(".left-seat"), "click", function(e) {
                    YAHOO.gosearch.app.getGameHall().sitdown(e, "left");
                });
                Event.addListener(q(".right-seat"), "click", function(e) {
                    YAHOO.gosearch.app.getGameHall().sitdown(e, "right");
                });
                that.refresh();

            };
            var callback = {
                success : loadViewSuccess,
                argument : {}
            };
            YAHOO.gosearch.Common.asyncRequest("get",
                VIEW_URL + "gameHall.php", callback);
        },

        /*
		 * add a new room
		 */
        add : function() {
            var that = this;
            var loadViewSuccess = function(props) {
                if (props.responseText == "success") {
                    alert(props.responseText);
                }
            };
            var callback = {
                success : loadViewSuccess,
                argument : {}
            };
            var postData = "method=addRoom";
            YAHOO.gosearch.Common.asyncRequest("post", SERVICE_URL
                + "playgo.php", callback, postData);
        },

        /*
		 * user log in
		 */
        login : function(e) {
            var that = this;
            var target = Event.getTarget(e);
            var form = Dom.getAncestorByTagName(target, "form");
            var username = form['username'].value;
            var password = form['password'].value;

            var loadViewSuccess = function(props) {
                if (props.responseText == "success") {
                    var el_game_hall = Dom.get('game-hall-container');
                    that.render(el_game_hall);
                } else {
                    var error = Sel.query(".error", form, true);
                    error.style.display = "block";
                }
            };
            var callback = {
                success : loadViewSuccess,
                argument : {}
            };
            var postData = "username=" + username + "&password=" + password
            + "&method=login";
            YAHOO.gosearch.Common.asyncRequest("post", SERVICE_URL
                + "playgo.php", callback, postData);
        },

        /*
		 * user log out
		 */
        logout : function(e) {
            var that = this;

            var loadViewSuccess = function(props) {
                if (props.responseText == "success") {
                    var el_game_hall = Dom.get('game-hall-container');
                    that.render(el_game_hall);
                } else {
                    var error = Sel.query(".error", form, true);
                    error.style.display = "block";
                }
            };
            var callback = {
                success : loadViewSuccess,
                argument : {}
            };
            var postData = "method=logout";
            YAHOO.gosearch.Common.asyncRequest("post", SERVICE_URL
                + "playgo.php", callback, postData);
        },

        sitdown : function(e,seat) {
            var that = this;
            var target = Event.getTarget(e);
            var desk = Dom.getAncestorByTagName(target, "div");
            var deskid=Dom.getAttribute(desk, "id");
            deskid = deskid.substr(5);
			
            var loadViewSuccess = function(props) {
                if (props.responseText == "success") {
                    that.refresh();
                }
            };
            var callback = {
                success : loadViewSuccess,
                argument : {}
            };
            var postData = "method=sitdown&deskid="+deskid+"&seat="+seat;
            YAHOO.gosearch.Common.asyncRequest("post", SERVICE_URL
                + "playgo.php", callback, postData);
        },
		
        refresh : function(){
            var that = this;
            var desk_array = q(".desk");

            var loadViewSuccess = function(props) {
                var json = props.responseText;
                var reply = YAHOO.lang.JSON.parse(json);
                for(var i = 0; i < desk_array.length; i++){
                    var left_seat = q(".left-seat",desk_array[i],true);
                    var right_seat = q(".right-seat",desk_array[i],true);
                    left_seat.innerHTML = reply[i]['left_user_id'];
                    right_seat.innerHTML = reply[i]['right_user_id'];
                }
            };
            var callback = {
                success : loadViewSuccess,
                argument : {}
            };
            var postData = "method=refresh";
            YAHOO.gosearch.Common.asyncRequest("post", SERVICE_URL
                + "playgo.php", callback, postData);
        }
    }

})();
